"""
给定一个字符串数组words，请计算当两个字符串 words[i] 和 words[j] 不包含相同字符时，它们长度的乘积的最大值。
假设字符串中只包含英语的小写字母。如果没有不包含相同字符的一对字符串，返回 0。

示例1:
输入: words = ["abcw","baz","foo","bar","fxyz","abcdef"]
输出: 16 
解释: 这两个单词为 "abcw", "fxyz"。它们不包含相同字符，且长度的乘积最大。

示例 2:
输入: words = ["a","ab","abc","d","cd","bcd","abcd"]
输出: 4 
解释: 这两个单词为 "ab", "cd"。

示例 3:
输入: words = ["a","aa","aaa","aaaa"]
输出: 0 
解释: 不存在这样的两个单词。

链接：https://leetcode-cn.com/problems/aseY1I
"""
from mode import *


class Solution:
    def maxProduct(self, words: List[str]) -> int:
        n = len(words)
        flags = [[False for _ in range(26)] for _ in range(n)]
        for i in range(n):
            for j in words[i]:
                flags[i][ord(j) - ord('a')] = True

        result = 0
        for i in range(n):
            for j in range(i + 1, n):

                for k in range(26):
                    if flags[i][k] and flags[j][k]:
                        break
                    if k == 25:
                        prod = len(words[i]) * len(words[j])
                        result = max(result, prod)
        return result


class Solution1:
    def maxProduct(self, words: List[str]) -> int:
        n = len(words)
        nums = [[False for _ in range(26)] for _ in range(n)]
        print(nums)
        for i in range(n):
            for j in words[i]:
                num = ord(j) - ord('a')
                nums[i][num] = True
        print(nums)
        res = 1
        for i in range(n):
            for j in range(i, n):
                for k in range(26):
                    if nums[i][k] and nums[j][k]:
                        break
                    if k == 25:
                        prod = len(words[i]) * len(words[j])
                        res = max(res, prod)

        return res


if __name__ == "__main__":
    A = Solution()
    print(A.maxProduct(["abcw", "baz", "foo", "bar", "fxyz", "abcdef"]))
    A = Solution1()
    print(A.maxProduct(["abcw", "baz", "foo", "bar", "fxyz", "abcdef"]))
